<?php
/**
 * User: b179a9
 * Date: 10/4/11
 * Time: 5:56 PM
 */

class App_Form_Items_Form extends Zend_Form
{

    public function init()
    {
        $this->setAction('');
        $this->setMethod('post');

        $id = new Zend_Form_Element_Hidden('id');
        $this->addElement($id);

        $name = new Zend_Form_Element_Text('name');
        $name->setRequired()
                ->setLabel('Your name:')
                ->addValidators(array(new Zend_Validate_StringLength(array('min' => 4, 'max' => 255))));
        $this->addElement($name);

        $email = new Zend_Form_Element_Text('email');
        $email->setRequired()
                ->setLabel('Email:')
                ->addValidators(array(new Zend_Validate_EmailAddress(),
                                     new Zend_Validate_StringLength(array('min' => 4, 'max' => 255))));
        $this->addElement($email);

        $phone = new Zend_Form_Element_Text('phone');
        $phone->setRequired()
                ->setLabel('Phone:')
                ->addValidators(array(new Zend_Validate_StringLength(array('min' => 4, 'max' => 255))));
        $this->addElement($phone);

        $content = new Zend_Form_Element_Textarea('content');
        $content->setLabel('Text:')
                ->setRequired();
        $this->addElement($content);

        $rubricsManager = new \Managers\ItemsRubrics();
        $rubrics = $rubricsManager->getList();

        $rubric = new Zend_Form_Element_Select('rubric');
        $rubric->setRequired()
                ->setLabel('Main rubric:')
                ->setAttrib('id', 'form-rubric');
        $defaultRubric = null;

        foreach ($rubrics as $record) {
            if (is_null($defaultRubric)) {
                $defaultRubric = $record['id'];
            }
            $rubric->addMultiOption($record['id'], $record['name']);
        }
        $this->addElement($rubric);

        $subrubrics = $rubricsManager->getList($defaultRubric);

        $subrubric = new Zend_Form_Element_Select('subrubric');
        $subrubric->setRequired()
                ->setLabel('Subrubric:')
                ->setAttrib('id', 'form-subrubric');
        foreach ($subrubrics as $record) {
            $subrubric->addMultiOption($record['id'], $record['name']);
        }
        $this->addElement($subrubric);

        $imgDir = "../public/images/captcha/";

        // check captcha path is writeable
        if (is_writable($imgDir)) {

            $captcha = new Zend_Form_Element_Captcha('captcha', array(
                                                                     'label' => _('Write symbols from image:'),
                                                                     'captcha' => 'Image',
                                                                     'captchaOptions' => array(
                                                                         'captcha' => 'Image',
                                                                         'wordLen' => 6,
                                                                         'timeout' => 300,
                                                                         'imgDir' => $imgDir,
                                                                         'imgUrl' => '/images/captcha',
                                                                         'suffix' => '.c.png',
                                                                         'font' => dirname(APPLICATION_PATH) .
                                                                                   "/assets/fonts/Glasten_Bold.ttf",
                                                                     )
                                                                ));

        } else {
            $captcha = new Zend_Form_Element_Captcha('captcha', array(
                                                                     'label' => "Write symbols from image:",
                                                                     'captcha' => 'Figlet',
                                                                     'captchaOptions' => array(
                                                                         'wordLen' => 6,
                                                                         'timeout' => 300,
                                                                     ),
                                                                ));
        }
        //        $this->addElement($captcha);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $this->addElement($submit);

        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));

    }

    public function populate($values)
    {
        parent::populate($values);
    }

    public function isValid($data)
    {
        $rubric_id = $data['rubric'];
        $rubricsManager = new \Managers\ItemsRubrics();

        $subrubric = $this->getElement('subrubric');
        $subrubrics = $rubricsManager->getList($rubric_id);
        $subrubric->clearMultiOptions();
        foreach ($subrubrics as $record) {
            $subrubric->addMultiOption($record['id'], $record['name']);
        }
        return parent::isValid($data);
    }
}
